package com.itheima.servlet;

import com.itheima.utils.CheckCodeUtil;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取输出流
        ServletOutputStream outputStream = resp.getOutputStream();
        //生成验证码
        String code = CheckCodeUtil.generateVerifyCode(4);
        //将验证码放在会话域中
        req.getSession().setAttribute("checkCode", code);
        //参数：宽，高，输出流，验证码
        CheckCodeUtil.outputImage(100,50,outputStream,code);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
